<?php
namespace Cyfy\Modules\BasicContent\Forms;

use \Cyfy\Message;
use \Cyfy\Modules\Forms;
use \Cyfy\Modules\BasicContent\Content;
use \Cyfy\Modules\BasicContent\Module;

/**
 * Creates a new block.
 */
class AddBlock extends Forms\Form{

	function structure()
	{
        $this -> permission('BasicContent', 'add new');
        
		$i = new Forms\TextBox("title", self :: t(20));
		$i -> required = true;
		$i -> maxlength = 255;
		$this -> addItem($i);
		
		$w = new Forms\WysiwygEditor("content", self :: t(40));
		$w -> required = true;
		$this -> addItem($w);
		
		$b = new Forms\Button(self :: t(510), Forms\Button::OK);
		$this -> addButton($b);		
    }
  
	function submit()
	{  
		// creating content
        $content = new Content($this -> get("title"), $this -> get("content"));
        $bcid = $content -> save();
		
		if(!$bcid)
		{
            // if error
            Message :: set(self :: t(511, array($this -> get("title"))), Message::ERROR);
            return false;
        }
			
        // show message
        Message :: set(self :: t(521, array($this -> get("title"))));
        
        // redirect?
        $this -> onSubmit = "if(confirm('" . self :: t(531, array($this -> get("title"))) . "')){ Cyfy.redirectTo('administration/Templates/setBlocks/?module=BasicContent&block={$bcid}'); }";
        
        return true;
	}
}
